package com.zzyk.web.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.aspectj.annotation.OperaLog;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.CommonCount;
import com.zzyk.main.model.pojo.MaintenanceInfo;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.DateAndString;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.DeviceService;
import com.zzyk.main.service.MaintenanceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author wss_PC
 * @DESCRIPTION 维保控制器
 * @create 2020-6-23
 */
@RestController
@RequestMapping("/maintenance")
public class MaintenanceController extends BaseController {
    @Autowired
    private MaintenanceInfoService maintenanceInfoService;

    @Autowired
    private DeviceService deviceService;

    /**
     * 根据设备ID查询维保情况维保倒计时，上次维保时间
     *
     * @param id
     * @return
     */
    @GetMapping("/getMaintenanceInfoByDId")
    public DateAndString getMaintenanceInfoByDId(Integer id) {
        return maintenanceInfoService.selectMaintenanceInfoByDId(id);
    }

    /**
     * 获取本月设备维保状态
     *
     * @return
     */
    @GetMapping("/getMaintenanceStatusThisMonth")
    public List<CommonCount> getMaintenanceStatusThisMonth() {
        /*return maintenanceInfoService.findMaintenanceStatusThisMonth();*/
        return null;
    }


    /**
     * 添加或者修改采集设备维保信息
     *
     * @param maintenanceInfo
     * @return
     */
    @OperaLog("添加或者修改采集设备维保信息")
    @PostMapping("/saveOrUpdateCollectionMaintenance")
    public Message saveOrUpdateCollectionMaintenance(MaintenanceInfo maintenanceInfo) throws UserException {
        if (maintenanceInfo.getMethodType() == 1) {
            maintenanceInfoService.updateByPrimaryKeySelective(maintenanceInfo);
            return Message.success();
        }
        maintenanceInfo.setType(0);
        maintenanceInfoService.insertSelective(maintenanceInfo);
        return Message.success();
    }

    @GetMapping("/countMaintenanceInfoByParam")
    public Message countMaintenanceInfoByParam(String dateTime, Integer type, Integer eId, String name, Integer deviceType) throws UserException {
        String beginTime = "";
        String endTime = "";
        if (StringUtils.isNotBlank(dateTime)) {
            String[] split = dateTime.split(" ~ ");
            beginTime = split[0];
            endTime = split[1];
        }
        SysUser user = currentUser();
        String[] areaArr = user.getArea();
        return Message.success()
                .addData("data", maintenanceInfoService.countMaintenanceInfoByParam(beginTime, endTime, type,eId, areaArr, name, deviceType, user.getDeviceList()));

    }

    /**
     * 根据ID查询维保数据
     *
     * @param id
     * @return
     */
    @GetMapping("/getMaintenanceBayId")
    public MaintenanceInfo getMaintenanceBayId(Integer id) {
        return maintenanceInfoService.selectByPrimaryKey(id);
    }

    /**
     * 删除维保数据
     *
     * @param id
     * @return
     */
    @OperaLog("删除维保记录")
    @GetMapping("/deleteMaintenance")
    public Message deleteMaintenance(Integer id) {
        System.out.println(id);
        try {
            maintenanceInfoService.deleteMaintenanceByPrimaryKey(id);
            return Message.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Message.failed("删除失败");
        }
    }

    /**
     * 根据维保时间查询记录 参数为空查询全部
     *
     * @param dateTime
     * @param type     区分生产和采集
     * @return
     */
    @GetMapping("/getMaintenanceInfoByParam")
    public Message getMaintenanceInfoByParam(String dateTime, Integer type, Integer eId, String name, Integer deviceType) throws UserException {
        String beginTime = "";
        String endTime = "";
        if (StringUtils.isNotBlank(dateTime)) {
            String[] split = dateTime.split(" ~ ");
            beginTime = split[0];
            endTime = split[1];
        }
        SysUser user = currentUser();
        String[] areaArr =user.getArea();
        Integer[] deviceList = user.getDeviceList();
        setPage();
        PageInfo<MaintenanceInfo> info = new PageInfo<>(maintenanceInfoService.selectAllByParam(beginTime, endTime, type, eId, areaArr, name, deviceType, deviceList));
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }


    /**
     * 添加生产设备维保信息并进行文件上传，应该可以共用。
     *
     * @param maintenanceInfo
     * @return
     */
    @OperaLog("添加或者修改生产设备维保")
    @PostMapping("/saveOrUpdateMaintenance")
    public Message saveOrUpdateMaintenance(MaintenanceInfo maintenanceInfo) throws UserException, IOException {
        if (maintenanceInfo.getMethodType() == 1) {
            maintenanceInfo.setUpdateTime(DateUtil.parseDateTime(DateUtil.now()));
            maintenanceInfoService.updateByPrimaryKeyAndFileUpload(maintenanceInfo, null);
            return Message.success();
        }
        maintenanceInfoService.insertMaintenanceInfo(maintenanceInfo, null);
        return Message.success();

    }


    @RequestMapping("list")
    public Message getMaintenance() throws UserException {
        SysUser sysUser = currentUser();
        return Message.success()
                .addData("data", deviceService.getMaintenanceList(sysUser.getArea(), null, sysUser.getDeviceList()))
                .addData("extension", deviceService.getExtensionMaintenanceInfo(sysUser.getArea(), null, sysUser.getDeviceList()));
    }
}
